<?php

class AppController extends Controller {

    var $components = array('Acl', 'Auth', 'Session', 'Cookie');

    //var $helpers = array('PaypalIpn.Paypal');
    function beforeFilter() {
        //Configure AuthComponent
        $uniqueIDCoookie = $this->Cookie->read('uniqueID');
        if (!empty($uniqueIDCoookie)) {
            $this->loadModel('Cart');
            $chkUniqueID = $this->Cart->find("count", array("conditions" => array('unique_id' => $uniqueIDCoookie, "status" => 0)));
            if (!$chkUniqueID)
                $this->generateUniqueID($uniqueIDCoookie);
        }else {
            $this->generateUniqueID();
        }

        $this->Auth->authorize = 'actions';
        $this->Auth->autoRedirect = true;
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'plugin' => null, 'admin' => false);
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'plugin' => null, 'admin' => false);
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'common');
        $this->Auth->userScope = array('User.active' => 1);
        $this->Auth->actionPath = 'controllers/';

        if ($this->Auth->User('id')) {
            $this->loadModel('Product');
            $this->loadModel('Cart');
            $this->set('total_productlist', $this->Product->find('count', array('conditions' => array('Product.user_id' => $this->Auth->User('id'),
                            'OR' => array(array('Product.products_status' => 'Active'), array('Product.products_status' => 'Approved'))))));
            $seller_productess = $this->Product->find("list", array('conditions' => array("Product.user_id" => $this->Auth->user('id')), 'fields' => array('Product.id')));
            $conditions = array('Cart.product_id' => $seller_productess, 'Cart.order_id >' => 0, 'Cart.payment_status' => 1);
            $this->set('totalsoldproductess', $this->Cart->find("count", array("conditions" => $conditions)));
        }
        //Set the cookie for browse and search left menu
        $this->set('browseByCategoryViewCookie', $this->Cookie->read('browseByCategoryView'));
        $this->set('browseByBrandViewCookie', $this->Cookie->read('browseByBrandView'));
        $this->set('browseBySoldViewCookie', $this->Cookie->read('browseBySoldView'));
        $this->set('browseByUnsoldViewCookie', $this->Cookie->read('browseByUnsoldView'));
        $this->set('browseByAllViewCookie', $this->Cookie->read('browseByAllView'));
        $this->set('browseByColorViewCookie', $this->Cookie->read('browseByColorView'));
        $this->set('browseBySizeViewCookie', $this->Cookie->read('browseBySizerView'));
        $this->set('browseByStyleViewCookie', $this->Cookie->read('browseByStyleView'));
        $this->set('browseByConditionViewCookie', $this->Cookie->read('browseByConditionView'));
         
    }

    function generateUniqueID($id=null) {
        $this->loadModel('Cart');
        if (empty($id))
            $uniqueID = Security::hash(time(), null, true);
        else
            $uniqueID = $id;
        /*
          if($this->Auth->User('id'))
          $this->data['Cart']['user_id']=$this->Auth->User('id');

          $this->data['Cart']['unique_id']=$uniqueID;
          $this->data['Cart']['add_date']=date("Y-m-d H:i:s");
          $this->Cart->create();
          if($this->Cart->save($this->data)){
          $this->Cookie->write('uniqueID',$uniqueID,true,'+1 weeks');
          }
         */
        $this->Cookie->write('uniqueID', $uniqueID, true, '+1 weeks');
    }

}